package com.suntop.carousel.qlmediaplay.video.ui;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.PixelFormat;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;


import com.suntop.carousel.qlmediaplay.tools.MeasureHelper;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@SuppressLint("NewApi")
public class SurfaceRenderView extends SurfaceView implements
        IRenderView, SurfaceHolder.Callback{
    private MeasureHelper mMeasureHelper;
    @Nullable
    private SurfaceHolder surfaceHolder;
    private IRenderViewListener iRenderViewListener;
    public SurfaceRenderView(Context context) {
        this(context,null);
    }

    public SurfaceRenderView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public SurfaceRenderView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        if (mMeasureHelper == null)
            mMeasureHelper = new MeasureHelper();
        if (surfaceHolder == null) {
            surfaceHolder = getHolder();
        }
        surfaceHolder.addCallback(this);
        surfaceHolder.setFormat(PixelFormat.RGBA_8888);
    }

    @Override
    public void surfaceCreated(@NonNull SurfaceHolder surfaceHolder) {
        if (this.surfaceHolder==null){
            this.surfaceHolder=surfaceHolder;
        }
    }

    @Override
    public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {
        this.surfaceHolder=holder;
        if (iRenderViewListener!=null){
            iRenderViewListener.setSurfaceHolder(surfaceHolder);
        }
    }

    @Override
    public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
        if (iRenderViewListener!=null){
            iRenderViewListener.destroySurfaceView(holder.getSurface());
        }
    }

    @Override
    public void addIRenderViewListener(IRenderViewListener listener) {
         iRenderViewListener=listener;
    }

    @Override
    public void setVideoSize(int videoWidth, int videoHeight) {
        if (videoWidth > 0 && videoHeight > 0) {
            mMeasureHelper.setVideoSize(videoWidth, videoHeight);
            requestLayout();
        }
    }

    @Override
    public void setVideoRotation(int degree) {
        mMeasureHelper.setVideoRotation(degree);
        setRotation(degree);
    }

    @Override
    public void setScaleType(int scaleType) {
        mMeasureHelper.setScreenScale(scaleType);
        requestLayout();
    }

    @Override
    public View getView() {
        return this;
    }

    @Override
    public Bitmap doScreenShot() {
        return null;
    }


    @Override
    public void release() {
        if (surfaceHolder != null) {
            surfaceHolder.getSurface().release();
            surfaceHolder.removeCallback(this);
        }
        surfaceHolder=null;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int[] measuredSize = mMeasureHelper.doMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(measuredSize[0], measuredSize[1]);
    }

}
